class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }
public class test {
    public static int sumNumbers(TreeNode root) {
        return sum(root, 0);
    }

    public static int sum(TreeNode node, int num) {

        if(node.left == null && node.right == null){
            return node.val + num * 10;
        }
        num = node.val + num * 10;
        return sum(node.left, num)+
                sum(node.right, num);

    }
    public static void main(String[] args) throws ClassNotFoundException {
        TreeNode treeNode = new TreeNode(4);
        treeNode.left = new TreeNode(9);
        treeNode.right = new TreeNode(0);
        treeNode.left.left = new TreeNode(5);
        treeNode.left.right = new TreeNode(1);
        int i = sumNumbers(treeNode);
        System.out.println(i);


    }
}
